package cn.edu.jxau.test;

import cn.edu.jxau.util.Stack;

public class Main {

    public static void main(String[] args) {
        
        Stack<Integer> stack = new Stack<>();
        stack.push(4);
        stack.push(5);
        stack.push(3);
        stack.push(7);
        stack.push(1);
        stack.push(9);
        System.out.println(stack);
        sortStack(stack);
        System.out.println(stack);
    }
    
    private static <T extends Comparable<T>> void sortStack(Stack<T> stack) {
        
        Stack<T> aux = new Stack<>();
        while(!stack.isEmpty()) {
            T top = stack.pop();
            while(!aux.isEmpty() && top.compareTo(aux.peek())>0) {
                stack.push(aux.pop());
            }
            aux.push(top);
        }
        
        while(!aux.isEmpty()) {
            stack.push(aux.pop());
        }
    }
}